
package ch.hearc.securiteSysteme.serie2.ex2ChallengeResponse.server;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;


public class Handling
	{
	
	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/
	
	//input
	private Server_I m_server;
	
	//simulation BDD
	private String m_userName;
	private String m_pass;
	private String m_nbOnce;
	
	//tools
	private STEP m_step;
	
	public enum STEP
		{
		READY, RECEPTION_USERNAME, RECEPTION_HASH
		};
	
	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public Handling(Server_I server) throws IOException, NoSuchAlgorithmException
		{
		m_step = STEP.READY;
		m_userName = "Davy";
		m_pass = "tototo";
		
		m_server = server;
		this.createListeners();
		server.start();
		}
	
	private void createListeners()
		{
		m_server.setOnMessageListener(new OnMessageListener_I()
			{
				
				@Override
				public void onNotification(String notification)
					{
					System.out.println(notification);
					}
				
				@Override
				public void onMessageReceived(Object obj)
					{
					//Début de conversation et recéption de user
					if (m_step == STEP.READY)
						{
						stepReceptionUser(obj);
						}
					//Réception du hash complet
					else if (m_step == STEP.RECEPTION_USERNAME)
						{
						stepHashReception(obj);
						}
					}
				
				private void stepHashReception(Object obj)
					{
					if (obj instanceof Byte[])
						{
						m_step = STEP.READY;
						try
							{
							//Hashage coté server
							String serverHash = m_pass + m_nbOnce;
							MessageDigest md = MessageDigest.getInstance("SHA-512");
							byte[] tabServerHash = serverHash.getBytes();
							md.update(tabServerHash);
							byte[] tabDigestedServerHash = md.digest();
							
							StringBuffer hexServerHash = new StringBuffer();
							for(int i = 0; i < tabDigestedServerHash.length; i++)
								{
								hexServerHash.append(Integer.toHexString(0xFF & tabDigestedServerHash[i]));
								}
							
							//client Hash
							Byte[] tmpTabClientHash = (Byte[])obj;
							byte[] tabClientHash = new byte[tmpTabClientHash.length];
							StringBuffer hexClientHash = new StringBuffer();
							for(int i = 0; i < tmpTabClientHash.length; i++)
								{
								hexClientHash.append(Integer.toHexString(0xFF & tmpTabClientHash[i]));
								}
							System.out.println("HASH client = " + hexClientHash.toString());
							System.out.println("HASH server = " + hexServerHash.toString());
							
							for(int i = 1; i <= tmpTabClientHash.length; i++)
								{
								tabClientHash[i - 1] = tmpTabClientHash[i - 1];
								}
							if (MessageDigest.isEqual(tabDigestedServerHash, tabClientHash))
								{
								System.out.println("envoi true");
								m_server.send(true);
								}
							else
								{
								System.out.println("envoi false");
								m_server.send(false);
								}
							}
						catch (NoSuchAlgorithmException e)
							{
							e.printStackTrace();
							}
						}
					else
						{
						String msgError = "String (hash) non reçu";
						System.err.println(msgError);
						m_server.send(msgError);
						}
					}
				
				private void stepReceptionUser(Object obj)
					{
					if (obj instanceof String)
						{
						String user = (String)obj;
						System.out.println("message reçu : " + user);
						
						//Si le user existe
						if (user.equals(m_userName))
							{
							m_step = STEP.RECEPTION_USERNAME;
							
							//Création et envoi d'un nombre aléatoire
							Random r = new Random();
							Integer nbOnce = new Integer(r.nextInt());
							m_nbOnce = nbOnce.toString();
							System.out.println("envoi nbOnce : " + nbOnce);
							m_server.send(nbOnce.toString());
							}
						else
							{
							String msgError = "user not exists";
							System.err.println(msgError);
							m_server.send("user not exists");
							}
						}
					else
						{
						String msgError = "String (user) non reçu";
						System.err.println(msgError);
						m_server.send(msgError);
						}
					}
				
				@Override
				public void onClientDisconnected(Server_I server, Object obj)
					{
					// TODO Auto-generated method stub
					
					}
				
				@Override
				public void onClientConnected(Server_I server)
					{
					System.out.println("client connecté !");
					
					}
			});
		}
	}
